﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：DBulletinArticle.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：公告文章信息数据访问层
// ===================================================================
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Pattern;
using System.Data;

namespace GDIT.DAL
{
    public class DBulletinArticle : IBulletinArticle
    {

        private XpoHelper.Test<Mtb_BulletinArticle> userOperation = null;

        /// <summary>
        /// 管理员展示页面
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectAllBulletinByAdmin()
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            return userOperation.Operation();
        }

        /// <summary>
        /// 查询可见字段为否的待审批记录
        /// </summary>
        /// <returns></returns>
        public IDataReader SelectIsVisibleByXZ()
        {
            return XpoHelper.SelectID("select * from (select OID, Title, PublishDate, IsVisible from dbo.Mtb_BulletinArticle union select OID, Title, PublishDate, IsVisible from dbo.Mtb_CollegeInformationArticle )t where t.IsVisible = '否'");
        }

        /// <summary>
        /// 新增加一篇公告
        /// </summary>
        /// <param name="bulletin">需要增加的公告对象</param>
        /// <returns>1：新增成功  0：新增失败</returns>
        public int InsertBulletinArticle(Mtb_BulletinArticle bulletin)
        {
            return XpoHelper.SaveAdded(bulletin);
        }

        /// <summary>
        /// 根据关键字查询并返回公告集合
        /// </summary>
        /// <param name="keyWord">输入的关键字</param>
        /// <returns></returns>
        public XPCollection SelectBulletinByKeyWord(string keyWord)
        {
            XPCollection xp = new XPCollection(typeof(Mtb_BulletinArticle), new ContainsOperator("KeyWords",new BinaryOperator("AritlceKeyWord","%"+keyWord+"%",BinaryOperatorType.Like)));
            return xp;
        }

        /// <summary>
        /// 删除一篇公告
        /// </summary>
        /// <param name="bulletin">需要删除的公告对象</param>
        /// <returns>1：删除成功  0：删除失败</returns>
        public int DeleteBulletinArticle(int id)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            return userOperation.Delete(id);
        }

        /// <summary>
        /// 更新一篇公告
        /// </summary>
        /// <param name="bulletin">需要更新的公告的对象</param>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateBulletinArticle(Mtb_BulletinArticle bulletin)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", bulletin.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Title = bulletin.Title;
                b.Contents = bulletin.Contents;
                b.Top = bulletin.Top;
                b.Important = bulletin.Important;
                b.Internal = bulletin.Internal;
                b.KeyWords.AddRange(bulletin.KeyWords);
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 增加公告的点击率
        /// </summary>
        public int AddBulletinArticleCounter(int id)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", id);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Counter = b.Counter + 1;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 查询所有非置顶，重点，内部的公告列表
        /// </summary>
        /// <returns>返回所有的公告列表</returns>
        public XPCollection SelectAllBulletinArticle()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Important", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 查询所有非置顶，重点，是内部的公告列表
        /// </summary>
        /// <returns>返回所有的公告列表</returns>
        public XPCollection SelectAllInternalBulletinArticle()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Important", "否", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 根据公告Id查询并返回公告单一记录
        /// </summary>
        /// <param name="id">需要查询的公告Id</param>
        /// <returns>返回公告单一记录</returns>
        public Mtb_BulletinArticle SelectBulletinArticleById(int id)
        {
            Mtb_BulletinArticle bulletin = null;
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", id);
            foreach (Mtb_BulletinArticle b in xp)
            {
                bulletin = b;
            }
            return bulletin;
        }

        /// <summary>
        /// 根据部门名称查询并返回该部门的所有公告列表
        /// </summary>
        /// <param name="name">需要查询的部门名称</param>
        /// <returns>返回该部门的公告列表</returns>
        public XPCollection SelectBulletinArticleByDepartment(string departmentName)
        {
            XPCollection collection = new XPCollection(typeof(Mtb_BulletinArticle),
      new ContainsOperator("Publisher", new ContainsOperator("Department", new BinaryOperator("DepartmentName", departmentName, BinaryOperatorType.Equal))));
            return collection;
        }

        /// <summary>
        /// 显示置顶公告
        /// </summary>
        /// <returns>返回置顶公告列表</returns>
        public XPCollection SelectBulletinArticleByTop()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示重点公告
        /// </summary>
        /// <returns>返回重点公告列表</returns>
        public XPCollection SelectBulletinArticleByImportant()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Important", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "否", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示内部置顶公告
        /// </summary>
        /// <returns>返回置顶公告列表</returns>
        public XPCollection SelectInternalBulletinArticleByTop()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Top", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 显示内部重点公告
        /// </summary>
        /// <returns>返回重点公告列表</returns>
        public XPCollection SelectInternalBulletinArticleByImportant()
        {
            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Important", "是", BinaryOperatorType.Equal));
            criteria.Operands.Add(new BinaryOperator("Internal", "是", BinaryOperatorType.Equal));
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation(criteria);
            //按照发布时间进行降序排列
            SortProperty sp = new SortProperty("PublishDate", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 设置公告为内部显示
        /// </summary>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateBulletinArticleByInternal(Mtb_BulletinArticle bulletin)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", bulletin.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Internal = bulletin.Internal;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 设置公告为重点显示
        /// </summary>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateBulletinArticleByImportant(Mtb_BulletinArticle bulletin)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", bulletin.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Important = bulletin.Important;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 设置公告为置顶显示
        /// </summary>
        /// <returns>1：更新成功  0：更新失败</returns>
        public int UpdateBulletinArticleByTop(Mtb_BulletinArticle bulletin)
        {
            userOperation = new XpoHelper.Test<Mtb_BulletinArticle>();
            XPCollection xp = userOperation.Operation("OID", bulletin.OID);
            foreach (Mtb_BulletinArticle b in xp)
            {
                b.Top = bulletin.Top;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 显示首页的公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectBulletinArticleByTopAndImportant()
        {
            XPCollection xp = new XPCollection(typeof(Mtb_BulletinArticle));
            xp.AddRange(SelectBulletinArticleByTop());
            xp.AddRange(SelectBulletinArticleByImportant());
            xp.AddRange(SelectAllBulletinArticle());
            return xp;
        }

        /// <summary>
        /// 显示内部首页的公告内容
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectInternalBulletinArticleByTopAndImportant()
        {
            XPCollection xp = new XPCollection(typeof(Mtb_BulletinArticle));
            xp.AddRange(SelectInternalBulletinArticleByTop());
            xp.AddRange(SelectInternalBulletinArticleByImportant());
            xp.AddRange(SelectAllInternalBulletinArticle());
            return xp;
        }
    }
}
